{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 列表的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "['张三', '李四', '王五']\n",
      "[123, '张三', True]\n",
      "['a', 'b', 'c', '1', '2', '3']\n"
     ]
    }
   ],
   "source": [
    "# 创建\n",
    "list1 = [1,2,3,4,5]\n",
    "maths = [95,85,77,100,65]\n",
    "list2 = ['张三','李四','王五']\n",
    "list3 = [123,'张三',True]\n",
    "list4 = list(\"abc123\")\n",
    "print(list1)\n",
    "print(list2)\n",
    "print(list3)\n",
    "print(list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三丰\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 4\u001b[0m\n\u001b[0;32m      2\u001b[0m list2[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m张三丰\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(list2[\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(list2[\u001b[38;5;241m3\u001b[39m])\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 访问 通过下标(索引)访问 从0开始\n",
    "list2[0] = \"张三丰\"\n",
    "print(list2[0])\n",
    "# print(list2[3])\n",
    "# 可以使用负数  从后往前数\n",
    "print(list2[-1])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三丰\n",
      "李四\n",
      "王五\n"
     ]
    }
   ],
   "source": [
    "# 遍历列表（逐个访问)\n",
    "for i in range(len(list2)):\n",
    "    print(list2[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[30, 40, 50, 60]\n",
      "[30, 40, 50, 60, 70, 80, 90, 100]\n",
      "[10, 20, 30, 40, 50, 60]\n",
      "[30, 50]\n",
      "[10, 30, 50, 70, 90]\n"
     ]
    }
   ],
   "source": [
    "# 列表的切片\n",
    "\n",
    "list5 = [10,20,30,40,50,60,70,80,90,100]\n",
    "# [M:N:K]\n",
    "# M 起始位置\n",
    "# N 终止位置(不含)\n",
    "# K 步长\n",
    "print(list5[2:6]) \n",
    "print(list5[2:]) \n",
    "print(list5[:6]) \n",
    "print(list5[2:6:2]) \n",
    "print(list5[::2]) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 列表相关函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "2000\n",
      "-9\n",
      "2754\n",
      "[-9, 3, 12, 43, 60, 95, 100, 105, 345, 2000]\n",
      "[12, 345, 43, 100, 3, 95, 60, 105, 2000, -9]\n"
     ]
    }
   ],
   "source": [
    "list6 = [12,345,43,100,3,95,60,105,2000,-9]\n",
    "print(len(list6))\n",
    "print(max(list6))\n",
    "print(min(list6))\n",
    "print(sum(list6))\n",
    "print(sorted(list6))\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 列表操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 345, 43, 100, 3, 95, 60, 105, 2000, -9, 10000, 100000]\n"
     ]
    }
   ],
   "source": [
    "list6.append(10000)\n",
    "list6.append(100000)\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-200, -100, -200, -100, 12, 345, 43, 100, 3, 95, 60, 105, 2000, -9, 10000, 100000]\n",
      "[-200, -100, -200, -99999, -100, 12, 345, 43, 100, 3, 95, 60, 105, 2000, -9, 10000, 100000]\n"
     ]
    }
   ],
   "source": [
    "list6.insert(0,-100)\n",
    "list6.insert(0,-200)\n",
    "print(list6)\n",
    "list6.insert(3,-99999)\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "list6.clear()\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 345, 43, 100, 3, 95, 60, 105]\n",
      "[12, 345, 43, 100, 3, 95, 60]\n",
      "[12, 345, 100, 3, 95, 60]\n"
     ]
    }
   ],
   "source": [
    "print(list6)\n",
    "list6.pop()\n",
    "print(list6)\n",
    "list6.pop(2)\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 345, 3, 95, 60]\n"
     ]
    }
   ],
   "source": [
    "list6.remove(100)\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[60, 95, 3, 345, 12]\n"
     ]
    }
   ],
   "source": [
    "list6.reverse()\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 12, 60, 95, 345]\n"
     ]
    }
   ],
   "source": [
    "list6.sort()\n",
    "print(list6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 12, 60, 95, 345]\n"
     ]
    }
   ],
   "source": [
    "list7 = list6.copy()\n",
    "print(list7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(list6.index(60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "list6.append(60)\n",
    "print(list6.count(60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 定义一个列表存放10个学生成绩\n",
    "# 2. 打印第3个学生的成绩\n",
    "# 3. 将第4个学生的成绩改为100分\n",
    "# 4. 遍历打印所有学生成绩\n",
    "# 5. 打印最高分和最低分和总分"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
